package com.suke.czx.modules.reward.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.RewardSaveGroup;
import com.suke.czx.common.validate.RewardUpdateGroup;
import com.suke.czx.modules.reward.entity.Reward;
import com.suke.czx.modules.reward.entity.RewardSearchBo;
import com.suke.czx.modules.reward.service.RewardService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 奖惩
 * 代码描述: 奖惩控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/03 0:32
 */
@Validated
@RestController
@RequestMapping("/api/reward")
@AllArgsConstructor
@Api(value = "RewardController", tags = "奖惩管理")
public class RewardController extends AbstractController {

    private final RewardService rewardService;

    /**
     * 分页查询奖惩列表
     * @param rewardSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:reward:list')")
    public R list(RewardSearchBo rewardSearchBo) {
        if (rewardSearchBo.getPage() == null || rewardSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询信息列表
        return rewardService.pageRewardList(rewardSearchBo);
    }

    /**
     * 保存奖惩
     * @param reward 奖惩
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存奖惩")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:reward:save')")
    public R save(@Validated(RewardSaveGroup.class) Reward reward, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        reward.setCreator(getUserId());
        reward.setCreateTime(new Date());
        rewardService.save(reward);
        return R.ok();
    }

    /**
     * 更新奖惩
     * @param reward 奖惩
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新奖惩")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:reward:update')")
    public R update(@Validated(RewardUpdateGroup.class) Reward reward, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        reward.setUpdater(getUserId());
        reward.setUpdateTime(new Date());
        rewardService.updateById(reward);
        return R.ok();
    }

    /**
     * 删除奖惩
     * @param rewardId 奖惩编码
     * @return VO
     */
    @SysLog("删除奖惩")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:reward:delete')")
    public R delete(@NotNull(message = "奖惩编码不能为空") Long rewardId) {
        return rewardService.updateDelReward(rewardId, getUserId());
    }

}
